package com.hwadee.springboot.controller.EmailController;

import com.hwadee.springboot.entity.EmailRequest;
import com.hwadee.springboot.entity.R;
import com.hwadee.springboot.service.impl.EmailServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class EmailController {

    @Autowired
    private EmailServiceImpl emailService;

    @PostMapping("/send-message")
    public R sendMessage(@RequestBody EmailRequest request) {
        String to = request.getParentEmail();
        String subject = "学生成绩通知";
        String htmlContent = "" +
                "<h1>尊敬的家长：</h1>" +
                "<p>您好！</p>" +
                "<p>学生 <strong>" + request.getStudentName() +
                "</strong> 在 <strong>" + request.getTestName() +
                "</strong> 中取得了 <strong>" + request.getScore() +
                "</strong> 分。</p>" +
                "<p>考试时间：" + request.getTestDate() + "</p>" +
                "<p>考试科目：" + request.getTestName() + "</p>" +
                "<p>教师寄语：<br>" + request.getTeacherMessage() + "</p>" +
                "<p>此致<br>敬礼！</p>" +
                "<p>教务处</p>";

        try {
            emailService.sendHtmlEmail(to, subject, htmlContent);
            return R.ok().msg("邮件发送成功");
        } catch (Exception e) {
            return R.error(500).msg("邮件发送失败: " + e.getMessage());
        }
    }
}